package com.viewlibrary.loadview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import com.viewlibrary.R;

public class ShapeLoadingView extends LinearLayout {
    private int distance;//动画移动距离
    private long duration=1000;//动画时间
    private ShapeView mShapeView;
    private View mShadowView;
    private boolean isStopAnimation=false;

    public ShapeLoadingView(Context context) {
        this(context,null);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        distance=dpTopx(80);
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_shape_loading, this);
        mShapeView=rootView.findViewById(R.id.shape_view);
        mShadowView=rootView.findViewById(R.id.shadow_view);
        rootView.post(new Runnable() {
            @Override
            public void run() {
                //下落动画
                startDownAnimation();
            }
        });
    }

    /**
     * 下落动画：形状下落先慢后快，阴影由小变大，下落结束后开启上升动画
     */
    private void startDownAnimation() {
        if (isStopAnimation)return;
        ObjectAnimator translateAnimation=ObjectAnimator.ofFloat(mShapeView,"translationY",0,distance);
        ObjectAnimator scaleAnimaton=ObjectAnimator.ofFloat(mShadowView,"scaleX",0.3f,1.0f);
        AnimatorSet set=new AnimatorSet();
        set.setDuration(duration);
        set.setInterpolator(new AccelerateInterpolator());
        set.playTogether(translateAnimation,scaleAnimaton);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
               //弹起动画
                startUpAnimation();
            }
        });
        set.start();
   }

    /**
     * 弹起动画：形状上升先快后慢，阴影由大变小
     */
    private void startUpAnimation() {
        if (isStopAnimation)return;
        ObjectAnimator translateAnimation=ObjectAnimator.ofFloat(mShapeView,"translationY",distance,0);
        ObjectAnimator scaleAnimaton=ObjectAnimator.ofFloat(mShadowView,"scaleX",1f,0.3f);
        AnimatorSet set=new AnimatorSet();
        set.setDuration(duration);
        set.setInterpolator(new DecelerateInterpolator());
        set.playTogether(translateAnimation,scaleAnimaton);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //下降
                startUpAnimation();
                //改变形状
                mShapeView.exChangeShape();
                //图形旋转动画
                startRotationAnimation();
            }
        });
        set.start();
    }

    /**
     * 图形旋转：圆形、正方形旋转180，三角形旋转120
     */
    private void startRotationAnimation() {
        ShapeView.Shape currentShape = mShapeView.getCurrentShape();
        ObjectAnimator rotation=null;
        switch (currentShape){
            case Circle:
            case Square:
                rotation=ObjectAnimator.ofFloat(mShapeView,"rotation",180,0);
                break;
            case Triangle:
                rotation=ObjectAnimator.ofFloat(mShapeView,"rotation",120,0);
                break;
        }
        rotation.setDuration(duration);
        rotation.setInterpolator(new DecelerateInterpolator());
        rotation.start();
    }

    private int dpTopx(int i) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i,getResources().getDisplayMetrics());
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(INVISIBLE);
        mShapeView.clearAnimation();
        mShadowView.clearAnimation();
        ViewGroup parent = (ViewGroup) getParent();
        if (parent!=null){
            parent.removeView(this);
            parent.removeAllViews();
        }
        isStopAnimation=true;
    }
}
